package com.pbc.gmp.action;

import com.pbc.gmp.commons.base.BaseController;
import com.pbc.gmp.dao.MenuResMapper;
import com.pbc.gmp.model.MenuTreeResourseDto;
import com.pbc.gmp.model.basemodel.MenuRes;
import com.pbc.gmp.service.IMenuResService;
import com.pbc.gmp.service.impl.MenuTreeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * <p>
 *   前端控制器
 * </p>
 *
 * @author kjk
 * @since 2017-04-17
 */
@Controller
@RequestMapping("/menures")
public class MenuResController extends BaseController {
    @Autowired
    IMenuResService menuResService;
    @Autowired
    private MenuTreeService menuTreeService;


    @RequestMapping(value="/manager")
    public String manager() {
        return "admin/menu/menu";
    }
    @RequestMapping(value="/menuList")
    @ResponseBody
    public List<MenuRes> menuList() {

        return menuResService.selectList(null);
    }
    @RequestMapping(value="/addPage")
    public String addPage() {
          return "admin/menu/menuAdd";
    }
    @RequestMapping(value="/menuListDto")
    @ResponseBody
    public List<MenuTreeResourseDto> menuListDto() {

        return menuResService.selectAllLeftMenu();
    }
    @RequestMapping(value = "/add", method = {RequestMethod.POST })
    @ResponseBody
    public Object menuAdd(MenuRes menuRes) {
       menuRes.setId(0L);
        System.out.print(menuRes.getMenurespid());
        if (menuRes.getMenurespid() ==null) {
            menuRes.setMenurespid(0);
        }
        if ( menuRes.getMenurespid() != 0) {
            String psn=menuResService.selectById(menuRes.getMenurespid()).getPsn();
            if ( psn.length() == 0) {

            }else {
                menuRes.setPsn(menuResService.selectById(menuRes.getMenurespid()).getPsn());
            }

        }
        menuResService.insert(menuRes);

       return renderSuccess("更新成功");
    }
    @RequestMapping(value="/editPage")
    public String editPage(Model model, @RequestParam Long id) {
        MenuRes menuRes= menuResService.selectById(id);
        model.addAttribute("menu", menuRes);

        return "admin/menu/menuEdit";
    }
    @RequestMapping(value = "/edit")
    @ResponseBody
    public Object menuEdit(MenuRes menuRes) {
        menuResService.updateById(menuRes);
      return renderSuccess("更新成功");
    }
    @RequestMapping(value = "/getPsn")
    @ResponseBody
    public String getPsn(@RequestParam("id") Long id) {

        return  menuResService.selectById(id).getPsn();
    }

    @RequestMapping(value="/resList")
    @ResponseBody
    public List<MenuTreeResourseDto> resList() {

        return menuResService.selectAllRes();
    }
}
